package com.zahb.qadx.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

/**
 * 提供一些获取应用信息的方法
 * Created by zhoudawei on 2016/12/5.
 */

public final class PackageUtils {

    /**
     * 获取App版本名称
     *
     * @param context 上下文
     * @return 版本名称
     */
    public static String getVersionName(Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);
            if (TextUtils.isEmpty(pi.versionName))
                return "";
            else
                return pi.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("VersionName", "Exception", e);
            return "";
        }
    }

    /**
     * 获取App版本号
     *
     * @param context 上下文
     * @return 版本号
     */
    public static int getVersionCode(Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);
            return pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("Versioncode", "Exception", e);
            return 1;
        }
    }

    /**
     * 获取App名称
     *
     * @param context 上下文
     * @return 名称
     */
    public static String getAppName(Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);
            ApplicationInfo info = pi.applicationInfo;
            return context.getResources().getString(info.labelRes);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("AppName", "Exception", e);
            return "";
        }
    }

}
